import MD5 from 'crypto-js/md5';

// 正则
export function callIsSuccess(msgCd) {
    let patt = /0{5}$/;
    return patt.test(msgCd);
}

export function  md5(arg,key) {
    const str = JSON.stringify(arg);
    let sign = '';
    sign = MD5(`${str}`+key).toString();
    return sign;
}

export function md5Plain(arg,key){
    return MD5(arg+key).toString();
}

export function resolveRspMsg($_self,rspMsg,callback){

  if(rspMsg.status==200){
    if ($_self.$commonFunc.callIsSuccess(rspMsg.data.msgCd)) {
      $_self.$message({
        message: '操作成功',
        type: 'success'
      });

      if(callback){
        callback(rspMsg.data.body);
      }

    }else{
      $_self.$message.error("error:"+rspMsg.data.msgCd);
    }
  }else{
    $_self.$message.error("error status:"+rspMsg.status);
  }

}

//页面菜单路径、配置管理
const routerMap=[
     {
       code :"10",
       name: '基础管理',
       name_en: 'base-manger',
       router: '',
       icon: 'el-icon-menu'},
     {
       code :"1001",
       name: '用户管理',
       name_en: 'userManger',
       router: '/user-manger',
       icon: 'el-icon-document'
     },
     {
       code :"1002",
       name: '菜单管理',
       name_en: 'menusManger',
       router: '/menus-manger',
       icon: 'el-icon-menu'
     }
  ];

export function buildMenTree(orginMenuTree) {
  for (var i=0;i<orginMenuTree.length;i++){
    for(var j=0;j<routerMap.length;j++) {
      if (orginMenuTree[i]['code'] == routerMap[j]['code']) {
        let item=routerMap[j];
        let children=orginMenuTree[i].children;
        orginMenuTree[i]=item;
        orginMenuTree[i].children=children;
        break;
      }
    }

    if(orginMenuTree[i].children!=null && orginMenuTree[i].children.length>0){
      buildMenTree(orginMenuTree[i].children)
    }
  }

  return orginMenuTree;
}
